package com.whut.transportation.overweight;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.whut.transportation.R;
import com.whut.transportation.adapter.GuDaoAdapter;
import com.whut.transportation.entity.AllTrackByStation;
import com.whut.transportation.entity.GuDao;
import com.whut.transportation.network.HttpManager;
import com.whut.transportation.network.HttpRequestVo;
import com.whut.transportation.parser.JSONParser;
import com.whut.transportation.ui.BaseActivity;
import com.whut.transportation.utils.Constant;


public class OWMainActivity extends BaseActivity{
	// implements SwipeRefreshLayout.OnRefreshListener
	private SharedPreferences preferences;
	
	private ListView gudaos;
	private GuDaoAdapter guDaoAdapter;
	private List<GuDao> gudaolist;
	//private SwipeRefreshLayout mSwipeLayout; 
	
	@Override
	public void onClick(View v) {
		super.onClick(v); 
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.activity_ow_main);
		setTitleBarView(false, "首页", -1, true);
		preferences = getSharedPreferences("userId", Context.MODE_PRIVATE);
	}
	
	@Override
	protected void findViewById() {
		gudaos = (ListView) findViewById(R.id.gudaos);
		//mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
	}

	@Override
	protected void afterFindViewId() {
		
	}

	@Override
	protected void processLogic() {
		getData();
	}
	
	private void getData(){
		gudaolist = new ArrayList<GuDao>();
		HttpRequestVo vo = new HttpRequestVo();
		vo.requestUrl = Constant.FIND_OW_TRACK_ALL_BY_STATION;
		vo.context = mContext;
		
		HashMap<String, String> req = new HashMap<String, String>();
		req.put("owstaffId", preferences.getString("userId", null));
		vo.requestDataMap = req;
		
		vo.parser = new JSONParser(AllTrackByStation.class);
		
		getDataFromServer(HttpManager.POST_MOTHOD, vo, 
				new DataCallback<HashMap<String, Object>>() {
					@Override
					public void onStart() {
					}
					@Override
					public void onFailed() {
						showNetErrorDialog();
					}
					@Override
					public void processData(
							HashMap<String, Object> paramObject,
							boolean paramBoolean) {
						AllTrackByStation allTrackByStation = (AllTrackByStation) paramObject.get("result");
						gudaolist.addAll(allTrackByStation.ListTrackByStation);
						GuDao g = gudaolist.get(0);
						if(!g.getGudao().equals("-1")){
							if(gudaos.getAdapter()==null){
								guDaoAdapter = new GuDaoAdapter(mContext, gudaolist, R.layout.activity_detail_gudao_item);
								gudaos.setAdapter(guDaoAdapter);
							} else {
								guDaoAdapter.notifyDataSetChanged();
							}
						} else {
							Toast.makeText(mContext, "无待处理条目", Toast.LENGTH_SHORT).show();
						}
					}
					@Override
					public void onFinish() {
						closeProgressDialog();
					}
				});
	}

	@Override
	protected void setListener() {
		//mSwipeLayout.setOnRefreshListener(this);
		//mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light,  
                //android.R.color.holo_orange_light, android.R.color.holo_red_light);
		gudaos.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
				if(gudaolist.size()>position){
					Intent intent = new Intent(OWMainActivity.this,OWTransInfoActivity.class);
					intent.putExtra("gudao", gudaolist.get(position));
					startActivity(intent);
				}
			}
		});
	}

	@Override
	protected void getDataAgain() {
	}
/*
	@Override
	public void onRefresh() {
		getData();
		guDaoAdapter.notifyDataSetChanged();		
	}*/
}
